home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3986 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  136 lines

  1. Path: mac147.maths.uwa.edu.au!user
  2. From: ma@maths.uwa.edu.au (ma)
  3. Newsgroups: comp.lang.c
  4. Subject: #define, question from a beginner
  5. Followup-To: comp.lang.c
  6. Date: 1 Feb 1996 08:39:08 GMT
  7. Organization: Math Dept, Univ of W A
  8. Distribution: world
  9. Message-ID: <ma-010296161301@mac147.maths.uwa.edu.au>
  10. NNTP-Posting-Host: mac147.maths.uwa.edu.au
  11.  
  12. The following is a hypothetical academic situation. Be grateful if you
  13. could help.
  14.  
  15. Consider the following example: Line numbers are NOT part of the program.
  16.  
  17. //===============================
  18. 1        #include <stdio.h>
  19. 2        void function(void);
  20. 3        
  21. 4        main(void)
  22. 5        {
  23. 6            function();
  24. 7        }
  25. 8        
  26. 9        void function(void)
  27. 10        {
  28. 11        short j=1,k=2;
  29. 12        printf("\ntesting macro\n");
  30. 13        
  31. 14            printf("%d, ",j);        // macro should include this first line
  32. 15                    /* comment is part of the macro
  33. 16                    the comment contains a few lines
  34. 17                    */
  35. 18            printf("%d",k);    // macro should include this last line
  36. 19            
  37. 20        printf("\nend of testing\n");
  38. 21        }
  39. //***************************
  40.  
  41. I can use #define to replace single lines#14 and 18. The following works.
  42.  
  43. //===============================
  44. #define first    printf("%d, ",j);        // macro should include this first line
  45. #define last        printf("%d",k);    // macro should include this last line
  46.  
  47. #include <stdio.h>
  48. void function(void);
  49.  
  50. main(void)
  51. {
  52.     function();
  53. }
  54.  
  55. void function(void)
  56. {
  57.     short j=1,k=2;
  58.     printf("\ntesting macro\n");
  59.     
  60.     first
  61.             /* comment is part of the macro
  62.             the comment contains a few lines
  63.             */
  64.     last
  65.     
  66.     printf("\nend of testing\n");
  67. }
  68. //***************************
  69.  
  70. QUESTION: Can I use one single macro fiveLines to replace lines from 14 to
  71. 18 inclusively? I tried the following but got rejected. Is there any way to
  72. get around it? Note that lines 14 and 18 contain local variables.
  73.  
  74.  
  75. //===============================
  76. #define fiveLines        printf("%d, ",j);        // macro should include this first
  77. line
  78.             /* comment is part of the macro
  79.             the comment contains a few lines
  80.             */
  81.         printf("%d",k);    // macro should include this last line
  82.  
  83.  
  84. #include <stdio.h>
  85. void function(void);
  86.  
  87. main(void)
  88. {
  89.     function();
  90. }
  91.  
  92. void function(void)
  93. {
  94.     short j=1,k=2;
  95.     printf("\ntesting macro\n");
  96.     
  97.     fiveLines
  98.     
  99.     printf("\nend of testing\n");
  100. }
  101. //***************************
  102.  
  103. Of course, you may suggest that I should use another function listed below
  104. but that is NOT my question. My question again: How can we use one single
  105. macro-name to replace a paragraph of several lines involving local
  106. variables and comments? Can we do it in C++?
  107.  
  108. //===============================
  109. void newFiveLines(short jj,short kk)
  110. {
  111.     printf("%d, ",jj);        // macro should include this first line
  112.             /* comment is part of the macro
  113.             the comment contains a few lines
  114.             */
  115.     printf("%d",kk);    // macro should include this last line
  116. }
  117.  
  118. void function(void)
  119. {
  120. short j=1,k=2;
  121. printf("\ntesting macro\n");
  122.     
  123.     newFiveLines(j,k);
  124.     
  125. printf("\nend of testing\n");
  126. }
  127. //***************************
  128.  
  129. =============
  130. Be grateful if you could reply to:
  131.  
  132.         ma@maths.uwa.edu.au
  133.  
  134. Thank you in advance. Gratefully.     ma
  135. =============
  136.